var ExamplesPagination;
(function($){
    ExamplesPagination = {
    	speed: 500,
    	easing: 'swing',
    	
    	move: function(){
    		var self = this;
    		
    		this.pageContainer.animate({
    			left: 0 - (this.current * $(this.pages[0]).width()) + "px"
    		}, {
    			speed: this.speed,
    			easing: this.easing,
    			complete: function(){
    				self.updatePages();
    			}
    		});
    	},
    	
    	goTo: function(ind){
    		this.current = Math.min(0, Math.max(this.total - 1, ind));
    		this.move();
    	},
    	
    	next: function(){
    		this.current = Math.min(this.total - 1, this.current + 1);
    		this.move();
    	},
    	
    	prev: function(){
    		this.current = Math.max(0, this.current - 1);
    		this.move();
    	},
    	
    	updatePages: function(){
    		if(this.current === 0){
    			this.prevBtn.addClass('disabled');
    		} else {
    			this.prevBtn.removeClass('disabled');
    		}
    		if(this.current == this.total - 1){
    			this.nextBtn.addClass('disabled');
    		} else {
    			this.nextBtn.removeClass('disabled');
    		}
    	},
    	
    	initialize: function(){
    		var self = this;
    		
    		this.pageContainer = $('#slidedeck_examples .slidenavigation .pages');
    		this.pages = $('#slidedeck_examples .slidenavigation .page');
    		this.prevBtn = $('#slidedeck_examples .slidenavigation a.btn_examples_prevnext.prev');
    		this.nextBtn = $('#slidedeck_examples .slidenavigation a.btn_examples_prevnext.next');
    		
    		this.total = this.pages.length;
    		this.current = 0;
    		
    		this.prevBtn.click(function(event){
    			event.preventDefault();
    			self.prev();
    		});
    		this.nextBtn.click(function(event){
    			event.preventDefault();
    			self.next();
    		});
    		
    		this.updatePages();
    	}
    };
    
    $(document).ready(function(){
		if(document.location.pathname.substr(0,9) == "/examples"){
			var gotoSlide = document.location.search.match(/\?hash=([a-zA-Z0-9\-_]+)/);
			if(gotoSlide !== null){
				window.scrollTo(0,0);
				SlideDeckExamples.goTo($('#slidedeck_examples dd').index($('#' + gotoSlide[1])));
			}

            var ssHeight = $('.slidedeck dd .screenshot').height();
            $('.slidedeck dd .screenshot img').each(function(){
                $(this).css({
                    marginTop: ((ssHeight - $(this).height()) / 2)
                });
            });
		}
        
		$('#slidedeck_examples .slidenavigation a.thumb').click(function(event){
			if($('#slidedeck_examples .slidenavigation').hasClass('index')){
				event.preventDefault();
				var example_id = this.href.split("#")[1];
				SlideDeckExamples.goTo($('#slidedeck_examples dd').index($('#' + example_id)) + 1);
                $(this).addClass('active').find('img').addClass('active');
			}
		});

		ExamplesPagination.initialize();
    });
})(jQuery);